package cn.ittaoli.tlzhuanzhuan.service;

import cn.ittaoli.tlzhuanzhuan.model.Result;
import cn.ittaoli.tlzhuanzhuan.model.User;
import cn.ittaoli.tlzhuanzhuan.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import utils.FileUploadUtils;
import utils.JsonUtils;
import utils.MD5Utils;

@Service
public class IndexService {
    @Autowired
    private UserRepository userRepository;


    public Result<User> login(String user,String password) {
        //User user = JsonUtils.jsonToPojo(param, User.class);
        //user.setPassword(MD5Utils.getMD5(user.getPassword()));
        password=MD5Utils.getMD5(password);
        User user1=userRepository.findByUsernameAndPassword(user,password);
        Result<User> result=new Result<User>();
        if(user1!=null){
            result.setCode(0);
            result.setData(user1);
        }else{
            result.setCode(1);
            result.setMsg("用户名或密码错误");
        }
        return result;
    }

    public Result<User> uploadHead(MultipartFile file,Integer id) {
        String path = FileUploadUtils.upload_qn_stream(file);


        User user=userRepository.findOne(id);
        user.setHeadpic(path);
        userRepository.save(user);


        Result<User> result=new Result<User>();
        result.setCode(0);
        result.setData(user);
        return result;

    }
}
